package com.shuzijun.leetcode.editor.cn;
public class ProductOfArrayExceptSelf{
    public static void main(String[] args) {
         Solution solution = new ProductOfArrayExceptSelf().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        public int[] productExceptSelf(int[] nums) {
            int n = nums.length;
            int[] result = new int[n];
            int leftProduct = 1;
            for (int i = 0; i < n; i++) {
                result[i] = leftProduct;
                leftProduct *= nums[i];
            }

            int rightProduct = 1;
            for (int i = n-1; i >= 0; i--) {
                result[i] *= rightProduct;
                rightProduct *= nums[i];
            }
            return result;
        }
//    public int[] productExceptSelf(int[] nums) {
//        int n = nums.length;
//        int[] leftProducts = new int[n];
//        int[] rightProducts = new int[n];
//        int product = 1;
//        for (int i = 0; i < n; i++) {
//            product *= nums[i];
//            leftProducts[i] = product;
//        }
//
//        product =1;
//        for (int i = n-1; i >= 0; i--) {
//            product *= nums[i];
//            rightProducts[i] = product;
//        }
//
//        int[] result = new int[n];
//        for (int i = 0; i < n; i++) {
//            result[i] = 1;
//            if (i-1 >= 0){
//                result[i] *= leftProducts[i-1];
//            }
//            if (i+1 < n){
//                result[i] *= rightProducts[i+1];
//            }
//        }
//
//        return result;
//    }
}
//leetcode submit region end(Prohibit modification and deletion)

}